use std::collections::HashMap;
use std::sync::{Arc, Mutex};

// use deno_core::serde_v8;
// use deno_core::v8;
// use deno_core::JsRuntime;
// use deno_core::RuntimeOptions;

use model_graph_types::container::workflow::{
    WorkflowBlockExecuteResultBuilder, WorkflowBlockExecuteResultStatus,
};
use model_graph_types::{
    container::workflow::{ScriptStatement, WorkflowBlockExecuteResult, WorkflowInputVariable},
    modeling::{Value, ValueType},
};

use crate::workflow::context::{Getter, WorkflowContext};

pub async fn execute(
    statement: &ScriptStatement,
    context: Arc<Mutex<WorkflowContext>>,
) -> anyhow::Result<WorkflowBlockExecuteResult> {
    Err(anyhow::anyhow!(""))
    // let result = _execute(statement, context).await;

    // match result {
    //     Ok(v) => Ok(WorkflowBlockExecuteResultBuilder::default()
    //         .status(WorkflowBlockExecuteResultStatus::Succeeded)
    //         .source_handle("source")
    //         .result(v)
    //         .build()?),
    //     Err(err) => Ok(WorkflowBlockExecuteResultBuilder::default()
    //         .status(WorkflowBlockExecuteResultStatus::Succeeded)
    //         .source_handle("fail-branch")
    //         .build()?),
    // }
}

// pub async fn _execute(
//     statement: &ScriptStatement,
//     context: Arc<Mutex<WorkflowContext>>,
// ) -> anyhow::Result<HashMap<String, Value>> {
//     let ScriptStatement {
//         script,
//         language,
//         variables,
//         outputs,
//         error_strategy,
//     } = statement;

//     let result: serde_json::Value = {
//         //
//         let mut runtime = JsRuntime::new(RuntimeOptions::default());

//         //组装参数
//         let mut args: HashMap<String, serde_json::Value> = HashMap::new();
//         for variable in variables {
//             args.insert(
//                 variable.variable.clone(),
//                 context
//                     .get_from_node(&variable.value_selector.1, &variable.value_selector.0)?
//                     .try_into()?,
//             );
//         }
//         //生成调用代码
//         let code = format!("{} \n main({})", script, serde_json::to_string(&args)?);
//         tracing::debug!("code: {code:?}");
//         if let Ok(output) = eval(&mut runtime, &code) {
//             tracing::debug!("Output: {output:?}");
//             Ok(output)
//         } else {
//             Err(anyhow::anyhow!("执行失败!"))
//         }
//     }?;
//     let output_value: Value = result.try_into()?;
//     let mut result: HashMap<String, Value> = HashMap::new();
//     for (key, _value_type) in outputs {
//         //从output中获取值
//         result.insert(
//             key.clone(),
//             output_value
//                 .value_from_key(key)
//                 .map_or(Value::None, |v| v.clone()),
//         );
//     }

//     Ok(result)
// }

// fn eval(context: &mut JsRuntime, code: &String) -> anyhow::Result<serde_json::Value> {
//     let res = context.execute_script("<anon>", code.clone());
//     match res {
//         Ok(global) => {
//             let scope = &mut context.handle_scope();
//             let local = v8::Local::new(scope, global);
//             let deserialized_value = serde_v8::from_v8::<serde_json::Value>(scope, local);

//             match deserialized_value {
//                 Ok(value) => Ok(value),
//                 Err(err) => Err(anyhow::anyhow!("Cannot deserialize value: {:?}", err)),
//             }
//         }
//         Err(err) => Err(anyhow::anyhow!("Evaling error: {:?}", err)),
//     }
// }
